// Import the page's CSS. Webpack will know what to do with it.

// Import libraries we need.
const Web3 = require('web3');
const contract = require('truffle-contract');
// Import our contract artifacts and turn them into usable abstractions.
const DenCarToken_artifacts = require('../../build/contracts/DenCarToken.json');
// MetaCoin is our usable abstraction, which we'll use through the code below.
var DenCarToken = contract(DenCarToken_artifacts);

// The following code is simple to show off interacting with your contracts.
// As your needs grow you will likely need to change its form and structure.
// For application bootstrapping, check out window.addEventListener below.

// window.App = {
//   start: function() {
//     var self = this;
//
//     // Bootstrap the MetaCoin abstraction for Use.
//     DenCarToken.setProvider(web3.currentProvider);
//
//     // Get the initial account balance so it can be displayed.
//     web3.eth.getAccounts(function(err, accs) {
//       if (err != null) {
//         alert("There was an error fetching your accounts.");
//         return;
//       }
//
//       if (accs.length == 0) {
//         alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
//         return;
//       }
//
//       accounts = accs;
//       account = accounts[0];
//       self.refreshBalance();
//       self.getInfo();
//
//     });
//   },
//
//   setStatus: function(message) {
//     var status = document.getElementById("status");
//     status.innerHTML = message;
//   },
//   getInfo: function() {
//     var address = document.getElementById("address");
//     var ETHbalance = document.getElementById("ETHbalance");
//     var balance = web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0]));
//     address.innerHTML = account;
//     ETHbalance.innerHTML = balance;
//     this.getIdInfo();
//   },
//   getIdInfo: function() {
//     var self = this;
//     var dct;
//     var name = document.getElementById("name");
//     var phone = document.getElementById("phone");
//
//
//     DenCarToken.deployed().then(function(instance) {
//       dct = instance;
//       return dct.getUserInfo.call(account,{from: account});
//     }).then(function(value) {
//
//         name.innerHTML = value[0];
//         phone.innerHTML = value[1];
//
//
//     }).catch(function(e) {
//       console.log(e);
//       self.setStatus("Error getting balance; see log.");
//     });
//   },
//


//
//   BuyCoin: function() {
//     var self = this;
//
//     var amount = parseInt(document.getElementById("buyAmount").value);
//
//     var dct;
//     DenCarToken.deployed().then(function(instance) {
//       dct = instance;
//       var data = "0xa6f2ae3a"; // must set data which contain the hash of function name
//
//       var trans = {"from":account,"to":dct.address,"value":web3.toWei(amount,"ether"),"data":data};
//       web3.eth.sendTransaction(trans);
//     });
//     this.refreshBalance();
//     this.getInfo();
//   },
//
//   SellCoin: function() {
//     var self = this;
//
//     var amount = parseInt(document.getElementById("SellAmount").value);
//
//     var dct;
//     DenCarToken.deployed().then(function(instance) {
//       dct = instance;
//       return dct.sell(amount, {from: account});
//     }).then(function() {
//       self.setStatus("Transaction complete!");
//       self.refreshBalance();
//     }).catch(function(e) {
//       console.log(e);
//       self.setStatus("Error sending coin; see log.");
//     });
//     this.refreshBalance();
//     this.getInfo();
//
//   },
//
//
//   UserRegister: function() {
//     var self = this;
//     var name = document.getElementById("UserName").value;
//     var phone = parseInt(document.getElementById("UserPhone").value);
//     var dct;
//     DenCarToken.deployed().then(function(instance) {
//
//       dct = instance;
//       // var name = web3.toHex(name);
//       // var phone = web3.toBigNumber(phone);
//       dct.userRegister(name,phone ,{from: account});
//     }).then(function() {
//       self.setStatus("Transaction complete!");
//       self.refreshBalance();
//       self.getInfo();
//     }).catch(function(e) {
//       console.log(e);
//       self.setStatus("Error sending coin; see log.");
//     });
//   },
//   //   DriverRegister: function() {
//   //   var self = this;
//   //   var dct;
//   //   DenCarToken.deployed().then(function(instance) {
//
//   //     dct = instance;
//   //     var name = web3.toHex("Driver A");
//   //     var phone = web3.toBigNumber("13121978864");
//   //     var idCard = web3.toBigNumber("522101");
//   //     var carBrand = web3.toHex("Benz");
//   //     var plateNumber = web3.toHex("yueB 123456");
//
//   //     dct.driverRegister(name,phone,idCard,carBrand,plateNumber ,{from: accounts[1]});
//   //   }).then(function() {
//   //     self.setStatus("Transaction complete!");
//   //     self.refreshBalance();
//   //     self.getInfo();
//   //   }).catch(function(e) {
//   //     console.log(e);
//   //     self.setStatus("Error sending coin; see log.");
//   //   });
//   // },
//
//
//   sendCoin: function() {
//     var self = this;
//
//     var amount = parseInt(document.getElementById("amount").value);
//     var receiver = document.getElementById("receiver").value;
//
//     this.setStatus("Initiating transaction... (please wait)");
//
//     var dct;
//     DenCarToken.deployed().then(function(instance) {
//       dct = instance;
//       return dct.transfer(receiver, amount, {from: account});
//     }).then(function() {
//       self.setStatus("Transaction complete!");
//       self.refreshBalance();
//     }).catch(function(e) {
//       console.log(e);
//       self.setStatus("Error sending coin; see log.");
//     });
//   },
//   //for go taxi
//   getPrice: function(){
//     var dct;
//     var self = this;
//     var price = document.getElementById("distance").value;
//     document.getElementById("go_distance").style.display="none";
//     document.getElementById("go_pre").style.display="block";
//     document.getElementById("dr_id").innerHTML = accounts[1];
//     document.getElementById("price").innerHTML = price;
//
//     DenCarToken.deployed().then(function(instance) {
//       dct = instance;
//       return dct.getDriverInfo.call(accounts[1],{from: accounts[1]});
//     }).then(function(value) {
//
//       document.getElementById("dr_name").innerHTML =  value[0];
//       document.getElementById("dr_phone").innerHTML = value[2];
//       document.getElementById("dr_carBrand").innerHTML = value[3];
//       document.getElementById("dr_plate").innerHTML = value[4];
//
//
//
//     }).catch(function(e) {
//       console.log(e);
//       self.setStatus("Error getting balance; see log.");
//     });
//   },
//   Pay: function() {
//     var self = this;
//     var dct;
//
//     var price = parseInt(document.getElementById("price").innerHTML);
//     var receiver = document.getElementById("dr_id").innerHTML;
//     document.getElementById("go_pre").style.display="none";
//     document.getElementById("set_comment").style.display="block";
//
//
//     DenCarToken.deployed().then(function(instance) {
//       dct = instance;
//       return dct.transfer(receiver, price, {from: account});
//     }).then(function() {
//       self.setStatus("Transaction complete!");
//       self.refreshBalance();
//     }).catch(function(e) {
//       console.log(e);
//       self.setStatus("Error sending coin; see log.");
//     });
//   },
//   comment: function(){
//     var dct;
//     var self = this;
//
//     var fromAddress = document.getElementById("address").innerHTML;
//     var toAddress = document.getElementById("dr_id").innerHTML;
//     var time = parseInt(new Date());
//     var price = parseInt(document.getElementById("price").innerHTML);
//     var comment = document.getElementById("comment").value;
//     var star = document.getElementById("star").value;
//
//     console.log(fromAddress);
//         console.log(time);
//     console.log(price);
//     console.log(comment);
//       console.log(star);
//
//
//     DenCarToken.deployed().then(function(instance) {
//       dct = instance;
//       return dct.storeTransaction(fromAddress, toAddress,time,price,comment,star, {from: account});
//     }).then(function() {
//       location.reload();
//       self.setStatus("Transaction complete!");
//       self.refreshBalance();
//     }).catch(function(e) {
//             location.reload();
//
//       console.log(e);
//       self.setStatus("Error sending coin; see log.");
//     });
//   }
// };


// window.addEventListener('load', function() {
//   // Checking if Web3 has been injected by the browser (Mist/MetaMask)
//   if (typeof web3 !== 'undefined') {
//     console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
//     // Use Mist/MetaMask's provider
//     window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
//   } else {
//     console.warn("No web3 detected. Falling back to http://127.0.0.1:7545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
//     // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
//     window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
//   }
//
//   App.start();
// });
if (typeof web3 !== 'undefined') {
    console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
    // Use Mist/MetaMask's provider
    web3 = new Web3(new Web3.providers.HttpProvider("http://111.230.254.250:8545"));
} else {
    // console.warn("No web3 detected. Falling back to http://127.0.0.1:7545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    web3 = new Web3(new Web3.providers.HttpProvider("http://111.230.254.250:8545"));
}


    // Bootstrap the MetaCoin abstraction for Use.
    DenCarToken.setProvider(web3.currentProvider);

exports.getDenCarBalance = function(address) {

    var balance = new Promise(function (resolve) {
        var dct;
        DenCarToken.deployed().then(function(instance) {
            dct = instance;
            console.log(address);
            resolve(dct.getBalance.call(address, {from: address}));
        }).catch(function(e) {
            console.log(e);
        });
    })
    return balance;
};

    // Get the initial account balance so it can be displayed.
var accounts;
web3.eth.getAccounts(function  (err, accs) {


    if (err != null) {
        console.log("There was an error fetching your accounts."+err);
        return;
    }

    if (accs.length == 0) {
        console.log("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
        return;
    }

    accounts = accs;
    console.log(accounts);
    exports.accounts = accounts;
});
exports.DenCarToken = DenCarToken;
